package com.google.android.apps.wakemewhere.ui;

import java.util.logging.Logger;

import com.google.android.apps.wakemewhere.IntentExtras;
import com.google.android.apps.wakemewhere.R;
import com.google.android.apps.wakemewhere.mapsearch.SearchResult;
import com.google.android.apps.wakemewhere.util.Preconditions;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class SearchResultsActivity extends ListActivity {
	
	private Logger logger = Logger.getLogger(getClass().getName());
	
	private SearchResult[] businesses = new SearchResult[0];
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        logger.warning("======================== onCreate");
        Parcelable[] results = getIntent().getParcelableArrayExtra(IntentExtras.RESULTS);
        if (results != null) {
        	businesses = new SearchResult[results.length];
        	System.arraycopy(results, 0, businesses, 0, results.length);
        	ListAdapter adapter = new BusinessArrayListAdapter(businesses);
        	setListAdapter(adapter);
        }
    }
    
    @Override
    public void onNewIntent(Intent intent) {
    	logger.warning("======================== onNewIntent");
    	
    }
    
    private class BusinessArrayListAdapter implements ListAdapter {
    	private final SearchResult[] businesses;
    	
    	BusinessArrayListAdapter(SearchResult[] businesses) {
    		Preconditions.nonNull(businesses);
    		this.businesses = businesses;
    	}

		public int getCount() {
			return businesses.length;
		}

		public Object getItem(int position) {
			return businesses[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public int getItemViewType(int position) {
			return 0;
		}
		
		private LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            System.out.println("getView " + position + " " + convertView + " type = " + type);
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflator.inflate(com.google.android.apps.wakemewhere.R.layout.mapsearch_row, null);
                holder.topTextView = (TextView)convertView.findViewById(R.id.toptext);
                holder.bottomTextView = (TextView)convertView.findViewById(R.id.bottomtext);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            SearchResult business = businesses[position];
            holder.topTextView.setText(business.getName());
            holder.bottomTextView.setText(business.getAddress());
            return convertView;
        }
	 		
		public int getViewTypeCount() {
			return 1;
		}

		public boolean hasStableIds() {
			return true;
		}

		public boolean isEmpty() {
			return businesses.length == 0;
		}

		public void registerDataSetObserver(DataSetObserver observer) {
		}

		public void unregisterDataSetObserver(DataSetObserver observer) {
		}

		public boolean areAllItemsEnabled() {
			return false;
		}

		public boolean isEnabled(int position) {
			return true;
		}	
    }
    
    private static class ViewHolder {
        public TextView topTextView;
        public TextView bottomTextView;
    }
	    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	Logger.global.warning("onListItemClick " + position + " " + businesses.length);
    	
    	if (position >= businesses.length || businesses[position] == null) {
    		return;
    	}
    	
    	businesses[position].writeOnto(getIntent());
    	setResult(RESULT_OK, getIntent());
    	finish();
    }
}
